var myFunc = require("./myCommonFunction.js");

var widthG = device.width;
var heightG = device.height
var findtime = 5000;
var waitTime = 17000;
// textContains("领喵币").findOne(findtime).click()
var list = [];

var w = floaty.window(
    <vertical>
        <button id="look" text="浏览" />
        <button id="zhifu" text="支付宝浏览" />
        <button id="nengliang" text="淘宝能量" />
        <button id="fuqi" text="福气" />
        <button id="stop" text="停止" />
    </vertical>
);

// w.setSize(600, 600);
// w.setTouchable(true);
w.setAdjustEnabled(true);
w.setPosition(50, 500);
w.exitOnClose();
setInterval(() => { }, 1000);


w.look.click(function () {
    var t = threads.start(function () {
        liulan3();
    });
    list.push(t);
});
w.zhifu.click(function () {
    var t = threads.start(function () {
        guang()
    });
    list.push(t);
});

w.nengliang.click(function () {
    var t = threads.start(function () {
        nengliang()
    });
    list.push(t);
});
w.fuqi.click(function () {
    var t = threads.start(function () {
        fuqi()
    });
    list.push(t);
});

function guang() {
    var a = textContains("去逛逛").findOne(3000);
    if (a != null) {
        a.click();
        sleep(20000);
        back();
        sleep(1000);
        var b = text("开心收下").findOne(1000);
        if (b != null) {
            b.click();
        }
        sleep(1000);
        guang();
    }
}
// taobao();
// liulan1();
var startIdx = -1;
function liulan3() {
    // var a = myFunc.findOnce('浏览', true);
    // if (a != null
    //     && a.length > 1) {
    //     a[1].click();
    //     sleep(22000);
    //     back();
    //     sleep(1500);
    //     liulan3();
    // }
    var a = myFunc.find('去完成');
    for (let idx = 0; idx < a.length; idx++) {
        if (idx == startIdx) {
            continue;
        }
        let b = a[idx];
        myFunc.clickCenter(b, 1)
        sleep(5000);
        if (desc('分享给好友').findOnce() != null) {
            myFunc.clickDesc('取消');
            sleep(1000);
            startIdx = idx;
            continue;
        }
        var lookWord = ['浏览15', '浏览得']
        for (var key of lookWord) {
            if(myFunc.findOnce(key, true) != null) {
                myFunc.waitToNotFind(key, true)
                break
            }
        }
        myFunc.tuichuToStr('注：')
        sleep(2000);
        liulan3();
    }
    // if (myFunc.pClick(myFunc.findOnce('浏览', true))) {

    // }
}

function nengliang() {
    doTask('去完成');
    doTask('去浏览');
    doTask('去完成');
    doTask('去浏览');
    log('结束')
}
var words = className("android.widget.TextView").boundsInside(54, 2350, widthG, heightG).find();
var nengliangBackWord = ['主会场', words[1].text()]
function getTask() {
    if (myFunc.findOnce('邀请好友') == null) {
        myFunc.clickR(widthG / 2, heightG - 50)
        sleep(2000);
        if (myFunc.findOnce('邀请好友') == null) {
            myFunc.tuichuToStr(nengliangBackWord, 1000, 1);
            while (scrollUp()) {

            }
            myFunc.clickR(600, 500)
            sleep(2000);
        }
    }
}


function doTask(taskBtn) {
    var a;
    do {
        getTask()
        a = myFunc.findOnce(taskBtn);
        if (a == null) {
            break;
        }
        myFunc.clickTextByBtn(taskBtn)
        if (myFunc.findOne('下滑', 3000, true) != null) {
            log('下滑浏览')
            scrollDown();
            sleep(16000);
        } else if (myFunc.findOne('已浏览商品', 2500) != null) {
            log('浏览10个商品')
            var b = myFunc.find('¥', true);
            log(b.length)
            for (let index = 0; index < 10; index++) {
                var element = b[index];
                if (!element.click()) {
                    myFunc.pClick(element)
                }
                sleep(4000);
                if (index < 9) {
                    myFunc.tuichuToStr("已浏览商品")
                }
                sleep(2000);
            }
        } else if (myFunc.findOnce('搜索', 1) != null) {
            log('搜索发现')
            myFunc.clickR(150, 549);
            myFunc.findOne('下滑', 2500, true);
            scrollDown();
            sleep(16000);
        } else {
            log('浏览')
            scrollDown();
            // else if (myFunc.findOne('推荐', 2500, true) != null) {
            //     log(2)
            //     myFunc.findOne('任务已完成', 25000)
            // } else if (myFunc.findOne('浏览中', 2500, true) != null) {
            //     log(4)
            myFunc.findOne('任务已完成', 25000)
        }
        var end = myFunc.findOnce('饿了么申请', 1) != null || myFunc.findOnce('广东省广州市', 1) != null
        myFunc.tuichuToStr(nengliangBackWord, 1000, 1);
        if (end){
            break
        }
        sleep(1000);
    } while (a != null)
}
function fuqi(){
    if(myFunc.findOnce('去邀请') == null) {
        myFunc.clickR(350, 2285);
        sleep(1000)
    }
    //点击15商品
    var q = myFunc.findOne('去浏览');
    if(q != null) {
        log('点击15商品')
        myFunc.clickCenter(q);
        sleep(2000);
        for (let index = 0; index < 5; index++) {
            scrollDown()
            sleep(2000)
        }
        var a = myFunc.find('预售价', 1);
        for (let idx = 0; idx < 15; idx ++) {
            myFunc.clickR(a[idx].text());
            sleep(5000);
            myFunc.tuichuToStr('预售价', 1000, 1)
            sleep(1000)
        }
       
    }
    q = myFunc.findOnce('去完成');
    if(q != null) {
        log('浏览15s')
        myFunc.clickCenter(q);
        sleep(3000);
        scrollDown();
        sleep(16000)
        back()
    }
    if(q != null) {
        sleep(2000);
        fuqi()
    }
}

function taobao() {
    try {
        myFunc.openMyApp("淘宝");
        myFunc.log("DA KAI LE MEIYOU")
        desc("签到").findOne(findtime);
        clickByText("推荐");
        myFunc.clickR(widthG - 100, heightG / 2);
        var rukou = textContains("赚糖").findOne(findtime);
        if (rukou == null) {
            myFunc.tuichu("淘宝");
            taobao();
        } else {

            rukou.click();
            textContains("去").findOne(findtime);
            sleep(3000);
            liulan1();
            var linqu = text("领取奖励").find();
            if (linqu != null && linqu.length > 0) {
                linqu.forEach(a => {
                    a.click();
                    sleep(1000);
                })
            }
            myFunc.tuichu("淘宝");
        }
    } catch (error) {
        myFunc.tuichu("淘宝");
        taobao();
    }
}

function liulan1() {
    sleep(1000);
    if (textContains("去").find().length <= 1) {
        log("无");
        return;
    }
    //'idx:0' 'depth:18' 'idx:0' 'depth:19'   'idx:1' 'depth:17' 
    var task = textContains("/").textMatches(".*\\d{1,2}/\\d{1,2}\\)").find();
    var task1 = task[0];
    var desc = className(task1.className()).indexInParent(task1.indexInParent()).depth(task1.depth() + 1).find();
    // var status = className("android.widget.Button").indexInParent(task1.indexInParent()+1).depth(task1.depth() - 1).find();
    myFunc.log(task.length, desc.length, 2);

    let dica = desc.length - task.length;
    for (let index = 0; index < task.length; index++) {
        var descText = desc[index + dica].text();
        var ttask = text(task[index].text()).findOnce();
        var taskText = ttask.text();
        myFunc.log(taskText, descText, "描述");

        if (taskText.indexOf("小鸡") != -1 || taskText.indexOf("小互动") != -1) {
            continue;
        }

        var numIdx = myFunc.getNumLimit(taskText);
        if (numIdx[0] == numIdx[1]) {
            continue;
        }
        if (descText.indexOf("15秒") != -1 || descText.indexOf("浏览") != -1) {
            waitTime = 17000;
        } else {
            waitTime = 1000;
        }

        for (let y = numIdx[0]; y < numIdx[1]; y++) {
            myFunc.clickCenter(ttask);
            liulan();
            sleep(1500);
        }
        liulan1();
        break;
    }


}

function see2() {
    var text = ".*浏览.*/.*";
    butSee(text);
    sleep(1000);
    text = ".*逛.*/.*"
    butSee(text);
    sleep(1000);
    text = ".*浏览.*/.*";
    butSee(text);
    sleep(1000);
    butSee(".*搜.*/.*");
    // sleep(1000);
    // butSee("/15")
    // sleep(1000);
    // butSee("/15")
}

function butSee(params) {
    myFunc.log(params);

    var actionList = textMatches(params).find();

    if (actionList.empty()) {
        myFunc.log("没有")
        return;
    }
    var action = actionList[0];
    var title = action.text();
    var numIdx = myFunc.getNumLimit(title);
    myFunc.log(title, numIdx, numIdx[0] == numIdx[1]);

    if (numIdx[0] == numIdx[1]) {
        return;
    }
    for (let idx = numIdx[0]; idx < numIdx[1]; idx++) {
        myFunc.log("liulan" + idx);
        myFunc.clickR(action.bounds().centerX(), action.bounds().centerY());
        var a = textStartsWith("逛满").findOne(1000);
        if (a != null) {
            var guanmanNumIdx = myFunc.getNumLimit(a.text());
            myFunc.log(a.text(), guanmanNumIdx, guanmanNumIdx[0] == guanmanNumIdx[1]);
            if (guanmanNumIdx[0] == guanmanNumIdx[1]) {
                return;
            }
            var b = textStartsWith("逛店最多").findOne(1000);
            var x = action.bounds().centerX();
            var y = action.bounds().centerY();
            for (let idx = guanmanNumIdx[0]; idx < guanmanNumIdx[1]; idx++) {
                myFunc.log("逛店" + idx);
                myFunc.clickR(x, y);
                liulan();
                sleep(1000);
            }
            back();
        } else {
            liulan();
        }
        sleep(1000);
    }
    butSee(params);
}

function liulan() {
    sleep(1500);

    var a = textContains("浏览得").findOne(findtime);
    if (a != null) {
        waitTime = 16000;
        textContains("已发放").findOne(waitTime);
    }
    // descMatches(".*任务.*完成").findOne(1000);
    sleep(1000);
    back();
    textContains("做任务").findOne(findtime).className();
}


function clickByText(str) {
    var a = textContains(str).findOne(5000);
    myFunc.clickCenter(a);
}

w.stop.click(function () {
    for (var element of list) {
        log(element)
        if (element != null) {
            element.interrupt();
        }
    }
    list = [];
});









